package com.feihong.activity.controller;

import com.feihong.activity.entity.JsonResult;
import com.feihong.activity.enums.SensorEnum;
import com.feihong.activity.service.PushCityService;
import com.feihong.activity.vo.PushVo;
import com.feihong.common.controller.DefaultController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 同步导入Controller
 * @author Filter
 */
@RestController
@RequestMapping("info")
public class PushCityController extends DefaultController {
    @Resource
    private PushCityService pushCityService;

    /**
     * 区向市同步接口
     * @param pushVO
     * @return
     * @throws IllegalAccessException
     */
    @PostMapping("city")
    public JsonResult pushInfoByArea(@RequestBody PushVo pushVO) throws IllegalAccessException {
            pushCityService.pushInfoByArea(pushVO);
        return new JsonResult();
    }

    /**
     * 区向市批量同步接口
     * @param pathName
     * @param batchName
     * @return
     * @throws IllegalAccessException
     * @throws IOException
     */
    @PostMapping("infos")
    public JsonResult pushInfosByArea(String pathName,String batchName) throws IllegalAccessException, IOException {
        pushCityService.pushAllByArea(pathName,batchName);
        return new JsonResult();
    }

    /**
     * 文件上传接口
     * @param file 上传的文件
     * @return url 文件路径
     * @throws IOException
     */
    @ResponseBody
    @PostMapping("/upload")
    public JsonResult upload(@RequestParam(value = "file") MultipartFile file) throws IOException {
        //返回存储路径
        String url = pushCityService.upload(file);
        return new JsonResult(url);
    }

    /**
     * 解析文件
     * @param pathName 文件路径
     * @return
     * @throws Exception
     */
    @PostMapping("/importFile")
    public JsonResult importFileByPath(String pathName) throws Exception {
        String result = pushCityService.importFileByPath(pathName, SensorEnum.IMPORT_TYPE_INSERT);
        return new JsonResult(result);

    }

    @PostMapping("importMapping")
    public JsonResult importMappingByPath(String pathName) throws Exception{
        pushCityService.importMappingByPath(pathName);
        return new JsonResult();
    }

    /**
     * 数据转换并推送至kafka
     * @param equipmentPathName
     * @param logPathName
     * @return
     * @throws Exception
     */
    @PostMapping("pushBaseInfoByFile")
    public JsonResult pushBaseInfoByFile(String equipmentPathName,String logPathName) throws Exception{
        pushCityService.pushBaseInfoByFile(equipmentPathName,logPathName);
        return new JsonResult();
    }

}
